package is.model.dao.search;

import is.NullUtil;
import is.model.bean.Employee;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author aspr0
 */
public class EmployeeSearch extends AbstractSearch
{
    /**
     *
     * @param strict whether to serach strict by departmentId
     * @param initialBean
     * @param connection
     * @return
     * @throws SQLException
     */
    public static List<Employee> search(Boolean strict, Employee initialBean, Connection connection) throws
            SQLException
    {
        String query;
        String where;
        PreparedStatement statement;

        if (strict)
        {
            where = "departmentId = ?";
            query = String.format(QUERY_CANVAS, initialBean.getTableName(), where);

            statement = connection.prepareStatement(query);
            statement.setInt(1, initialBean.getDepartmentId());
        }
        else
        {
            where = organizeRegex(NullUtil.toString(initialBean.getId()), initialBean.getFields(), initialBean.getValues());

            query = String.format(QUERY_CANVAS, initialBean.getTableName(), where);

            statement = connection.prepareStatement(query);
            Integer parameterN = 0;

            Integer id = initialBean.getId();
            /* If value isnt empty, it should be placed in query within regex mask */
            if (id != null)
            {
                parameterN++;
                statement.setString(parameterN, String.format(REGEX_CANVAS, id));
            }

            String lastName = initialBean.getLastName();
            if (lastName != null)
            {
                parameterN++;
                statement.setString(parameterN, String.format(REGEX_CANVAS, prepareMask(lastName)));
            }

            String firstName = initialBean.getFirstName();
            if (firstName != null)
            {
                parameterN++;
                statement.setString(parameterN, String.format(REGEX_CANVAS, prepareMask(firstName)));
            }

            String middleName = initialBean.getMiddleName();
            if (middleName != null)
            {
                parameterN++;
                statement.setString(parameterN, String.format(REGEX_CANVAS, prepareMask(middleName)));
            }

            Integer departmentId = initialBean.getDepartmentId();
            if (departmentId != null)
            {
                parameterN++;
                statement.setString(parameterN, String.format(REGEX_CANVAS, departmentId));
            }

            Integer telephone = initialBean.getTelephone();
            if (telephone != null)
            {
                parameterN++;
                statement.setString(parameterN, String.format(REGEX_CANVAS, telephone));
            }

            Float salary = initialBean.getSalary();
            if (salary != null)
            {
                parameterN++;
                statement.setString(parameterN, String.format(REGEX_CANVAS, salary));
            }
        }

        statement.execute();

        ResultSet resultSet = statement.getResultSet();

        List<Employee> beans = new LinkedList<Employee>();

        while (resultSet.next())
        {
            Employee listBean = (Employee) initialBean.clone();
            listBean.processResultSet(resultSet);
            beans.add(listBean);
        }
        resultSet.close();

        return beans;
    }
}
